import {Component, ViewChild} from '@angular/core';
import {Content, IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {LoadingService} from "../../../services/common/loading.service";
import {PerformanceType} from "../../../model/enum/performance";
import {TimeUtil} from "../../../model/util/TimeUtil";
import {TeamOverviewProvider} from "../../../providers/perfomance/team-overview/team-overview-provider";
import {ToggleList} from '../../../model/pages/performance/personal-performance';

@IonicPage()
@Component({
    selector:'page-team-performance-detail',
    templateUrl:'team-performance-detail.html',
    providers:[
        LoadingService,
        TeamOverviewProvider
    ]
})

export class TeamPerformanceDetailPage {
    @ViewChild(Content) content: Content;

    private title:string = null;
    private startFilterDate = '';
    private endFilterDate = '';
    private id='';
    private type:number = null;
    private personalPerformance:any={};
    // typeList
    typeList:ToggleList= new ToggleList();

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        private toastController: ToastController,
        private load:LoadingService,
        private teamOverViewProvider:TeamOverviewProvider,
    ){}

    ionViewDidLoad() {

        this.type = this.navParams.get('type');
        let month = this.navParams.get("month");
        this.title = this.navParams.get('groupName');
        this.id = this.navParams.get('groupID');
        //初始化时间范围（当月）

        this.startFilterDate = this.navParams.get('startFilterDate');
        this.endFilterDate = this.navParams.get('endFilterDate');

        this.filter();
    }

    filter(){
        if (this.endFilterDate != '' && this.startFilterDate != '' && this.endFilterDate < this.startFilterDate) {
            let toast = this.toastController.create({
                message: '结束时间不能小于开始时间，请重新选择',
                duration: 2000,
                position: 'middle'
            });
            toast.present();
            return;
        }
        this.load.showLoading();
        this.teamOverViewProvider.getPerformanceDetail(this.type,this.id,this.startFilterDate,this.endFilterDate).subscribe(r=>{
            this.load.dismissLoading();
            this.personalPerformance = r;
        },e=>{
            this.load.dismissLoading();
            this.personalPerformance = {};
        });
    }

    /**
     * 返回
     * @param ev
     */
    backButtonClick(ev) {
        ev.preventDefault();
        ev.stopPropagation();
        this.navCtrl && this.navCtrl.pop(null, null);
    }

    /**
     * 跳转至成员列表
     */
    goPersonalPerformanceListPage() {
        this.navCtrl.push('TeamPerformanceMemberPage', {
            groupID: this.id,
            groupName:this.title,
            startFilterDate:this.startFilterDate,
            endFilterDate:this.endFilterDate
        })
    }

    toggleType(num){
        this.typeList['type'+num]=!this.typeList['type'+num];

    }
}
